package com.example.demo.java8.lambda;

/*
 *  @ author  zengqingfa
 *  @ created in    2020/2/22  21:23
 *
 */

import org.junit.Test;

import java.util.Comparator;
import java.util.function.Consumer;

/*
 *  @Author    zengqingfa
 *  @Created in    2020/2/20  21:05
 *  @Description
 */
public class LambdaTest1 {

    @Test
    public void test1() {
        //匿名内部类的写法
//        定义了一个没有名字的类(匿名类)
//        这个类的参数参数，调用的是 Runnable 接口
//        我们通过 new Runnable(){...}的方式创建了这个类，并重写了该接口的 run() 方法
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程:" + Thread.currentThread().getName());
            }
        }).start();

        //lambda表达式写法
        new Thread(() -> System.out.println("线程：" + Thread.currentThread().getName())).start();
    }

    //无参无返回值
    @Test
    public void test2() {
        new Thread(() -> System.out.println("线程。。")).start();
    }

    //有参有返回值
    @Test
    public void test3() {
        Comparator<Integer> com = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };
        int compare = com.compare(21, 12);
        System.out.println(compare);
        System.out.println("***************");
        Comparator<Integer> com1 = (o1, o2) -> Integer.compare(o1, o2);
        System.out.println(com1.compare(12, 21));
    }

    //一个参数无返回值
    //数据类型可以省略，可以由编译器推断得出，类型推断
//    ArrayList<String> list=new ArrayList<>();
//    int[] arr=new int[]{1,2,34};
//    int[] arr2={1,2,3};
    @Test
    public void test4() {
        Consumer<String> con = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        con.accept("hello world");
        System.out.println("************");
        Consumer<String> con2 = s -> System.out.println(s);
        con2.accept("hello world2");
    }
}
